package com.industrial.im.controller;

import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.im.domain.DispatchApplyExtension;
import com.industrial.im.service.IDispatchApplyExtensionService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 申请延期Controller
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/extension")
public class DispatchApplyExtensionController extends BaseController
{
    @Autowired
    private IDispatchApplyExtensionService dispatchApplyExtensionService;

    /**
     * 查询申请延期列表
     */
    @RequiresPermissions("extension:extension:list")
    @GetMapping("/list")
    public TableDataInfo list(DispatchApplyExtension dispatchApplyExtension)
    {
//        public static final String PROVINCE = "p_01_011";
//        public static final String CITY = "s_01_011";
//        public static final String COUNTY = "x_01_011";
        startPage();
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
            dispatchApplyExtension.setCounty(sysUser.getDistrict());
        }else if(ArrayUtil.contains(roles.toArray(),"s_01_011")){
            dispatchApplyExtension.setCity(sysUser.getCity());
        }else if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
            dispatchApplyExtension.setProvince(sysUser.getProvince());
        }
        List<DispatchApplyExtension> list = dispatchApplyExtensionService.selectDispatchApplyExtensionList(dispatchApplyExtension);
        return getDataTable(list);
    }

    /**
     * 导出申请延期列表
     */
    @RequiresPermissions("extension:extension:export")
    @Log(title = "申请延期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DispatchApplyExtension dispatchApplyExtension)
    {
        List<DispatchApplyExtension> list = dispatchApplyExtensionService.selectDispatchApplyExtensionList(dispatchApplyExtension);
        ExcelUtil<DispatchApplyExtension> util = new ExcelUtil<DispatchApplyExtension>(DispatchApplyExtension.class);
        util.exportExcel(response, list, "申请延期数据");
    }

    /**
     * 获取申请延期详细信息
     */
    @RequiresPermissions("extension:extension:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(dispatchApplyExtensionService.selectDispatchApplyExtensionByBaseId(baseId));
    }

    /**
     * 新增申请延期
     */
    @RequiresPermissions("extension:extension:add")
    @Log(title = "申请延期", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DispatchApplyExtension dispatchApplyExtension)
    {
        return toAjax(dispatchApplyExtensionService.insertDispatchApplyExtension(dispatchApplyExtension));
    }

    /**
     * 修改申请延期
     */
    @RequiresPermissions("extension:extension:edit")
    @Log(title = "申请延期", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DispatchApplyExtension dispatchApplyExtension)
    {
        return toAjax(dispatchApplyExtensionService.updateDispatchApplyExtension(dispatchApplyExtension));
    }

    /**
     * 删除申请延期
     */
    @RequiresPermissions("extension:extension:remove")
    @Log(title = "申请延期", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(dispatchApplyExtensionService.deleteDispatchApplyExtensionByBaseIds(baseIds));
    }

    @PostMapping("/check")
    public AjaxResult check(@RequestBody DispatchApplyExtension dispatchApplyExtension){
        dispatchApplyExtensionService.check(dispatchApplyExtension);
        return AjaxResult.success();
    }
}
